package com.hxz.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hxz.pojo.User;
import com.hxz.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @RequestMapping("/j1")
    // @ResponseBody //不会走视图解析器，直接返回字符串
    public String json1(String name, Model model) throws JsonProcessingException {
        //jackson    ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        //创建对象
        User user = new User("哈哈哈", 3, "男");
        String s = mapper.writeValueAsString(user);
        return s.toString();
    }

    @RequestMapping("/j2")
    @ResponseBody //不会走视图解析器，直接返回字符串
    public String json2() throws JsonProcessingException {
        //jackson    ObjectMapper
        ObjectMapper mapper = new ObjectMapper();

        List<User> userList = new ArrayList<User>();
        //创建对象
        User user1 = new User("哈哈哈1", 3, "男");
        User user2 = new User("哈哈哈1", 3, "男");
        User user3 = new User("哈哈哈1", 3, "男");
        User user4 = new User("哈哈哈1", 3, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        String s = mapper.writeValueAsString(userList);
        return s;
    }


    @RequestMapping("/j3")
    @ResponseBody //不会走视图解析器，直接返回字符串
    public String json3() throws JsonProcessingException {

        Date date = new Date();

        return JsonUtils.getJson(date, "yyyy-MM-dd HH:mm:ss");
    }


    @RequestMapping("/j4")
    @ResponseBody //不会走视图解析器，直接返回字符串
    public String json4() throws JsonProcessingException {
        List<User> userList = new ArrayList<User>();
        //创建对象
        User user1 = new User("哈哈哈1", 3, "男");
        User user2 = new User("哈哈哈1", 3, "男");
        User user3 = new User("哈哈哈1", 3, "男");
        User user4 = new User("哈哈哈1", 3, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        return JsonUtils.getJson(userList);
    }



    @RequestMapping("/j5")
    @ResponseBody //不会走视图解析器，直接返回字符串
    public String json5() throws JsonProcessingException {
        List<User> userList = new ArrayList<User>();
        //创建对象
        User user1 = new User("哈哈哈1", 3, "男");
        User user2 = new User("哈哈哈1", 3, "男");
        User user3 = new User("哈哈哈1", 3, "男");
        User user4 = new User("哈哈哈1", 3, "男");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        String s = JSON.toJSONString(userList);


        return s;
    }

}
